//
//  AjenView.m
//  AjenKit
//
//  Created by Ajen on 2017/8/31.
//  Copyright © 2017年 中国·北京·洵. All rights reserved.
//
//
//                       .::::.
//                     .::::::::.
//                    ::::Ajen:::
//                 ..:::::::::::'
//              '::::::::::::'
//                .::::::::::
//           '::::::::::::::..
//                 .::::::::::::.
//                ::::::::::::::::
//               ::::``:::::::::'        .:::.
//              ::::'   ':::::'       .::::::::.
//            .::::'      ::::     .:::::::'::::.
//           .:::'       :::::  .:::::::::' ':::::.
//          .::'        :::::.:::::::::'      ':::::.
//         .::'         ::::::::::::::'         ``::::.
//     ...:::           ::::::::::::'              ``::.
//    ````              ':::::::::'                  ::::..
//                       '.:::::'                    ':'````..
//  [······view拓展······]
//

#import "AjenView.h"
#import <objc/runtime.h>

@interface UIView ()

@property void(^tapAction)(id);

@end

@implementation AjenView


@end

@implementation UIView (AjenView)

-(UIView *(^)(NSInteger  aj_tag))aj_tag{
    return ^id(NSInteger  aj_tag){
        self.tag = aj_tag;
        return self;
    };
}

-(UIView *(^)(UIView * aj_addToView))aj_addToView{
    return ^id(UIView * aj_addToView){
        [aj_addToView addSubview:self];
        return self;
    };
}

-(UIView *(^)(CGFloat  aj_alpha)) aj_alpha{
    return ^id(CGFloat aj_alpha){
        self.alpha = aj_alpha;
        return self;
    };
}

- (UIView *(^)(UIColor * aj_bgColor))aj_bgColor{
    return ^id(UIColor * aj_bgColor){
        self.backgroundColor = aj_bgColor;
        return self;
    };
}

- (UIView *(^)(NSString * aj_bgColorHexStr))aj_bgColorHexStr{
    return ^id(NSString * aj_bgColorHexStr){
        self.backgroundColor = [UIColor colorWithAjenHexString:aj_bgColorHexStr];
        return self;
    };
}

- (UIView *(^)(CGFloat  aj_borderWidth))aj_borderWidth{
    return ^id(CGFloat  aj_borderWidth){
        self.layer.masksToBounds = YES;
        self.layer.borderWidth = aj_borderWidth;
        return self;
    };
}

- (UIView *(^)(UIColor * aj_borderColor))aj_borderColor{
    return ^id(UIColor * aj_borderColor){
        self.layer.masksToBounds = YES;
        self.layer.borderColor = [aj_borderColor CGColor];
        return self;
    };
}

- (UILabel *(^)(NSString * aj_borderColorHexStr)) aj_borderColorHexStr{
    return ^id(NSString * aj_borderColorHexStr){
        self.layer.masksToBounds = YES;
        self.layer.borderColor = [[UIColor colorWithAjenHexString:aj_borderColorHexStr] CGColor];
        return self;
    };
}

- (UIView *(^)(CGFloat  aj_cornerRadius))aj_cornerRadius{
    return ^id(CGFloat  aj_cornerRadius){
        self.layer.masksToBounds = YES;
        self.layer.cornerRadius = aj_cornerRadius;
        return self;
    };
}

- (UIView *(^)(CGFloat  aj_frame_x))aj_frame_x{
    return ^id(CGFloat  aj_frame_x){
        CGRect frame = self.frame;
        frame.origin.x = aj_frame_x;
        self.frame = frame;
        return self;
    };
}

- (UIView *(^)(CGFloat  aj_frame_y))aj_frame_y{
    return ^id(CGFloat  aj_frame_y){
        CGRect frame = self.frame;
        frame.origin.y = aj_frame_y;
        self.frame = frame;
        return self;
    };
}

- (UIView *(^)(CGFloat  aj_frame_w))aj_frame_w{
    return ^id(CGFloat  aj_frame_w){
        CGRect frame = self.frame;
        frame.size.width = aj_frame_w;
        self.frame = frame;
        return self;
    };
}

- (UIView *(^)(CGFloat  aj_frame_h))aj_frame_h{
    return ^id(CGFloat  aj_frame_h){
        CGRect frame = self.frame;
        frame.size.height = aj_frame_h;
        self.frame = frame;
        return self;
    };
}

- (UIView *(^)(CGFloat  aj_frame_center_x))aj_frame_center_x{
    return ^id(CGFloat  aj_frame_center_x){
        CGPoint center = self.center;
        center.x = aj_frame_center_x;
        self.center = center;
        return self;
    };
}

- (UIView *(^)(CGFloat  aj_frame_center_y))aj_frame_center_y{
    return ^id(CGFloat  aj_frame_center_y){
        CGPoint center = self.center;
        center.y = aj_frame_center_y;
        self.center = center;
        return self;
    };
}

- (UIView *(^)(CGRect  aj_frameRect))aj_frameRect{
    return ^id(CGRect  aj_frameRect){
        self.frame = aj_frameRect;
        return self;
    };
}

-(void)displayTest{
    for(int i = 0 ; i < self.subviews.count ; i++){
        self.subviews[i].aj_borderWidth(1).aj_borderColorHexStr(@"#f50d85");
        if(self.subviews[i].subviews.count > 0){
            [self.subviews[i] displayTest];
        }
    }
}

///获取Xib View对象
+(id)loadXibString:(NSString *)xibName{
    return [[[NSBundle mainBundle]loadNibNamed:xibName owner:self options:nil] objectAtIndex:0];
}

- (void)setX:(CGFloat)X {
    CGRect frame = self.frame;
    frame.origin.x = X;
    self.frame = frame;
}

- (void)setY:(CGFloat)Y {
    CGRect frame = self.frame;
    frame.origin.y = Y;
    self.frame = frame;
}

- (CGFloat)X {
    return self.frame.origin.x;
}

- (CGFloat)Y {
    return self.frame.origin.y;
}

- (void)setCenterX:(CGFloat)CenterX {
    
    CGPoint center = self.center;
    center.x = CenterX;
    self.center = center;
    
}

- (CGFloat)CenterX {
    return self.center.x;
}

- (void)setCenterY:(CGFloat)CenterY{
    CGPoint center = self.center;
    center.y = CenterY;
    self.center = center;
}

- (CGFloat)CenterY {
    return self.center.y;
}

- (void)setWidth:(CGFloat)Width {
    CGRect frame = self.frame;
    frame.size.width = Width;
    self.frame = frame;
}

- (void)setHeight:(CGFloat)Height {
    CGRect frame = self.frame;
    frame.size.height = Height;
    self.frame = frame;
}

- (CGFloat)Height {
    return self.frame.size.height;
}

- (CGFloat)Width {
    return self.frame.size.width;
}

- (void)setSize:(CGSize)Size {
    CGRect frame = self.frame;
    frame.size = Size;
    self.frame = frame;
}

- (CGSize)Size {
    return self.frame.size;
}

- (void)setOrigin:(CGPoint)Origin {
    CGRect frame = self.frame;
    frame.origin = Origin;
    self.frame = frame;
}

- (CGPoint)Origin {
    return self.frame.origin;
}
- (CGFloat)Top {
    return self.frame.origin.y;
}

- (void)setTop:(CGFloat)Top {
    CGRect frame = self.frame;
    frame.origin.y = Top;
    self.frame = frame;
}

- (CGFloat)Left {
    return self.frame.origin.x;
}

- (void)setLeft:(CGFloat)Left {
    CGRect frame = self.frame;
    frame.origin.x = Left;
    self.frame = frame;
}


- (CGFloat)Bottom {
    return self.frame.size.height + self.frame.origin.y;
}

- (void)setBottom:(CGFloat)Bottom {
    CGRect frame = self.frame;
    frame.origin.y = Bottom - frame.size.height;
    self.frame = frame;
}

- (CGFloat)Right {
    return self.frame.size.width + self.frame.origin.x;
}

- (void)setRight:(CGFloat)Right {
    CGRect frame = self.frame;
    frame.origin.x = Right - frame.size.width;
    self.frame = frame;
}

- (BOOL)isDisplayedInScreen
{
    if (self == nil) {
        return FALSE;
    }
    
    CGRect screenRect = [UIScreen mainScreen].bounds;
    
    // 转换view对应window的Rect
    CGRect rect = [self convertRect:self.frame fromView:nil];
    if (CGRectIsEmpty(rect) || CGRectIsNull(rect)) {
        return FALSE;
    }
    
    // 若view 隐藏
    if (self.hidden) {
        return FALSE;
    }
    
    // 若没有superview
    if (self.superview == nil) {
        return FALSE;
    }
    
    // 若size为CGrectZero
    if (CGSizeEqualToSize(rect.size, CGSizeZero)) {
        return  FALSE;
    }
    
    // 获取 该view与window 交叉的 Rect
    CGRect intersectionRect = CGRectIntersection(rect, screenRect);
    if (CGRectIsEmpty(intersectionRect) || CGRectIsNull(intersectionRect)) {
        return FALSE;
    }
    
    return TRUE;
}

-(void)setTapAction:(void (^)(id))tapAction{
    objc_setAssociatedObject(self, @"AddClickedEvent", tapAction, OBJC_ASSOCIATION_COPY_NONATOMIC);
}
-(void (^)(id))tapAction{
    return objc_getAssociatedObject(self, @"AddClickedEvent");
}
-(void)addTapBlock:(void (^)(id))tapAction{
    self.tapAction = tapAction;
    // hy:先判断当前是否有交互事件，如果没有的话。。。所有gesture的交互事件都会被添加进gestureRecognizers中
    if (![self gestureRecognizers]) {
        self.userInteractionEnabled = YES;
        // hy:添加单击事件
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)];
        [self addGestureRecognizer:tap];
    }
}
- (void)tap{
    if (self.tapAction) {
        self.tapAction(self);
    }
}

-(void)setAJ_裁剪:(BOOL)AJ_裁剪{
    self.layer.masksToBounds = AJ_裁剪;
}

-(void)setAJ_圆角:(CGFloat)AJ_圆角{
    self.layer.cornerRadius = AJ_圆角;
}

-(void)setAJ_边框粗细:(CGFloat)AJ_边框粗细{
    self.layer.borderWidth = AJ_边框粗细;
}

-(void)setAJ_边框颜色:(UIColor *)AJ_边框颜色{
    self.layer.borderColor = AJ_边框颜色.CGColor;
}

@end

@implementation UILabel (AjenLabel)

- (UILabel *(^)(UIFont * aj_font)) aj_font{
    return ^id(UIFont * aj_font){
        self.font = aj_font;
        return self;
    };
}

- (UILabel *(^)(CGFloat  aj_fontFloat)) aj_fontFloat{
    return ^id(CGFloat  aj_fontFloat){
        self.font = [UIFont systemFontOfSize:aj_fontFloat];
        return self;
    };
}

- (UILabel *(^)(UIColor * aj_textColor)) aj_textColor{
    return ^id(UIColor * aj_textColor){
        self.textColor = aj_textColor;
        return self;
    };
}

- (UILabel *(^)(NSString * aj_textColorHexStr)) aj_textColorHexStr{
    return ^id(NSString * aj_textColorHexStr){
        self.textColor = [UIColor colorWithAjenHexString:aj_textColorHexStr];
        return self;
    };
}

- (UILabel *(^)(NSTextAlignment  aj_textAlignment)) aj_textAlignment{
    return ^id(NSTextAlignment  aj_textAlignment){
        self.textAlignment = aj_textAlignment;
        return self;
    };
}

- (UILabel *(^)(CGFloat  aj_numberOfLines)) aj_numberOfLines{
    return ^id(CGFloat  aj_numberOfLines){
        self.numberOfLines = aj_numberOfLines;
        return self;
    };
}


- (UILabel *(^)(NSString * aj_Text)) aj_Text{
    return ^id(NSString * aj_Text){
        self.text = aj_Text;
        return self;
    };
}


- (UILabel *(^)(CGFloat MaxWidth)) aj_AutoWidth{
    return ^id(CGFloat MaxWidth){
        self.preferredMaxLayoutWidth = MaxWidth;
        [self setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
        return self;
    };
}

- (UILabel *(^)(NSString * aj_DeleteStyleText)) aj_DeleteStyleText{
    return ^id(NSString * aj_DeleteStyleText){
        
        /**
         点进这里的勇士 , 你好 !
         请先设置文字颜色和文字Font (label.aj_textColorHexStr 和 label.aj_fontFloat) 再用此方法
         2017.9.15 Ajen
         */
        NSMutableAttributedString *attritu = [[NSMutableAttributedString alloc]initWithString:aj_DeleteStyleText];
        [attritu addAttributes:@{
                                 NSStrikethroughStyleAttributeName:@(NSUnderlineStyleThick),
                                 NSForegroundColorAttributeName:
                                     self.textColor,
                                 NSBaselineOffsetAttributeName:
                                     @(0),
                                 NSFontAttributeName: self.font
                                 } range:NSMakeRange(0, aj_DeleteStyleText.length)];
        self.attributedText = attritu;
        return self;
    };
}

@end


@implementation UITextField (AjenTextField)

- (UITextField *(^)(UIFont * aj_font)) aj_font{
    return ^id(UIFont * aj_font){
        self.font = aj_font;
        return self;
    };
}

- (UITextField *(^)(CGFloat  aj_fontFloat)) aj_fontFloat{
    return ^id(CGFloat  aj_fontFloat){
        self.font = [UIFont systemFontOfSize:aj_fontFloat];
        return self;
    };
}

- (UITextField *(^)(UIColor * aj_textColor)) aj_textColor{
    return ^id(UIColor * aj_textColor){
        self.textColor = aj_textColor;
        return self;
    };
}

- (UITextField *(^)(NSString * aj_textColorHexStr)) aj_textColorHexStr{
    return ^id(NSString * aj_textColorHexStr){
        self.textColor = [UIColor colorWithAjenHexString:aj_textColorHexStr];
        return self;
    };
}

- (UITextField *(^)(NSTextAlignment  aj_textAlignment)) aj_textAlignment{
    return ^id(NSTextAlignment  aj_textAlignment){
        self.textAlignment = aj_textAlignment;
        return self;
    };
}

///* * * * * 设置"完成"按钮文字 * * * * *
- (UITextField *(^)(NSString * aj_toolbarOKItemName)) aj_toolbarOKItemName{
    return ^id(NSString * aj_toolbarOKItemName){
        UIView *bar = [[UIView alloc] initWithFrame:CGRectMake(0,0, [UIScreen mainScreen].bounds.size.width,44)];
        bar.backgroundColor = [UIColor colorWithAjenHexString:@"#f5f5f5"];
        
        UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        button.aj_buttonTitle(aj_toolbarOKItemName).aj_buttonFont([UIFont systemFontOfSize:14 weight:UIFontWeightRegular]).aj_buttonTintColorHex(@"#21c6c8").aj_addTargetSelector(self,@selector(itemButtonDown)).aj_addToView(bar);
//        [button mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.top.bottom.equalTo(bar);
//            make.right.equalTo(bar).offset(-15);
//        }];
        button.frame = CGRectMake(bar.frame.size.width - 100, 0, 85, bar.frame.size.height);
        [bar layoutIfNeeded];
        [button setEnlarge:15];
        self.inputAccessoryView = bar;
        UIView * topLine = [[UIView alloc] initWithFrame:CGRectMake(0, 0, bar.frame.size.width, 0.5)];
        topLine.backgroundColor = [UIColor lightGrayColor];
        [bar addSubview:topLine];
        
        return self;
    };
}
-(void)itemButtonDown{
    [self resignFirstResponder];
}

@end


@implementation UITextView (AjenTextView)

- (UITextView *(^)(UIFont * aj_font)) aj_font{
    return ^id(UIFont * aj_font){
        self.font = aj_font;
        return self;
    };
}

- (UITextView *(^)(CGFloat  aj_fontFloat)) aj_fontFloat{
    return ^id(CGFloat  aj_fontFloat){
        self.font = [UIFont systemFontOfSize:aj_fontFloat];
        return self;
    };
}

- (UITextView *(^)(UIColor * aj_textColor)) aj_textColor{
    return ^id(UIColor * aj_textColor){
        self.textColor = aj_textColor;
        return self;
    };
}

- (UITextView *(^)(NSString * aj_textColorHexStr)) aj_textColorHexStr{
    return ^id(NSString * aj_textColorHexStr){
        self.textColor = [UIColor colorWithAjenHexString:aj_textColorHexStr];
        return self;
    };
}

- (UITextView *(^)(NSTextAlignment  aj_textAlignment)) aj_textAlignment{
    return ^id(NSTextAlignment  aj_textAlignment){
        self.textAlignment = aj_textAlignment;
        return self;
    };
}


///* * * * * 设置"完成"按钮文字 * * * * *
- (UITextView *(^)(NSString * aj_toolbarOKItemName)) aj_toolbarOKItemName{
    return ^id(NSString * aj_toolbarOKItemName){
        UIView *bar = [[UIView alloc] initWithFrame:CGRectMake(0,0, [UIScreen mainScreen].bounds.size.width,44)];
        bar.backgroundColor = [UIColor colorWithAjenHexString:@"#f5f5f5"];
        
        UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        button.aj_buttonTitle(aj_toolbarOKItemName).aj_buttonFont([UIFont systemFontOfSize:14 weight:UIFontWeightRegular]).aj_buttonTintColorHex(@"#21c6c8").aj_addTargetSelector(self,@selector(itemButtonDown)).aj_addToView(bar);
//        [button mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.top.bottom.equalTo(bar);
//            make.right.equalTo(bar).offset(-15);
//        }];
        button.frame = CGRectMake(bar.frame.size.width - 100, 0, 85, bar.frame.size.height);
        [bar layoutIfNeeded];
        [button setEnlarge:15];
        self.inputAccessoryView = bar;
        UIView * topLine = [[UIView alloc] initWithFrame:CGRectMake(0, 0, bar.frame.size.width, 0.5)];
        topLine.backgroundColor = [UIColor lightGrayColor];
        [bar addSubview:topLine];
        return self;
    };
}
-(void)itemButtonDown{
    [self resignFirstResponder];
}
@end


@implementation UIImageView(AjenImageView)

-(UIImageView * (^)(NSString * aj_imgName)) aj_imgName{
    return ^id(NSString * aj_imgName){
        self.image = [UIImage imageNamed:aj_imgName];
        return self;
    };
}

-(UIImageView * (^)(BOOL aj_imgIsFill))  aj_imgIsFill{
    return ^id(BOOL  aj_imgIsFill){
        if(aj_imgIsFill == YES){
            self.layer.masksToBounds = YES;
            self.contentMode = UIViewContentModeScaleAspectFill;
        }
        return self;
    };
}

@end







@implementation UIButton(AjenButton)
static char topNameKey;
static char rightNameKey;
static char bottomNameKey;
static char leftNameKey;
-(UIButton * (^)(SEL aj_selector)) aj_selector{
    return ^id(SEL aj_selector){
        [self addTarget:[self viewController] action:aj_selector forControlEvents:UIControlEventTouchUpInside];
        return self;
    };
}
- (UIViewController *)viewController {
    for (UIView* next = [self superview]; next; next = next.superview) {
        UIResponder *nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)nextResponder;
        }
    }
    return nil;
}
-(UIButton * (^)(id target ,SEL aj_selector)) aj_addTargetSelector{
    return ^id(id target ,SEL aj_selector){
        [self addTarget:target action:aj_selector forControlEvents:UIControlEventTouchUpInside];
        return self;
    };
}

-(UIButton * (^)(NSString * aj_buttonTitle)) aj_buttonTitle{
    return ^id(NSString * aj_buttonTitle){
        [self setTitle:aj_buttonTitle forState:UIControlStateNormal];
        return self;
    };
}

-(UIButton * (^)(UIFont * aj_buttonFont)) aj_buttonFont{
    return ^id(UIFont * aj_buttonFont){
        self.titleLabel.font = aj_buttonFont;
        return self;
    };
}

-(UIButton * (^)(CGFloat  aj_buttonFontFloat)) aj_buttonFontFloat{
    return ^id(CGFloat  aj_buttonFontFloat){
        self.titleLabel.font = [UIFont systemFontOfSize:aj_buttonFontFloat];
        return self;
    };
}

-(UIButton * (^)(UIColor * aj_buttonTintColor)) aj_buttonTintColor{
    return ^id(UIColor * aj_buttonTintColor){
        self.tintColor = aj_buttonTintColor;
        return self;
    };
}

-(UIButton * (^)(NSString * aj_buttonTintColorHex)) aj_buttonTintColorHex{
    return ^id(NSString * aj_buttonTintColorHex){
        self.tintColor = [UIColor colorWithAjenHexString:aj_buttonTintColorHex];
        return self;
    };
}

-(UIButton * (^)(UIImage * aj_buttonImg)) aj_buttonImg{
    return ^id(UIImage * aj_buttonImg){
        [self setImage:aj_buttonImg forState:UIControlStateNormal];
        return self;
    };
}

-(UIButton * (^)(NSString * aj_buttonImgName)) aj_buttonImgName{
    return ^id(NSString * aj_buttonImgName){
        [self setImage:[UIImage imageNamed:aj_buttonImgName] forState:UIControlStateNormal];
        return self;
    };
}

- (UIButton *(^)(CGFloat  aj_numberOfLines)) aj_numberOfLines{
    return ^id(CGFloat  aj_numberOfLines){
        self.titleLabel.numberOfLines = aj_numberOfLines;
        return self;
    };
}

-(void)setEnlarge:(CGFloat)enlarge{
    objc_setAssociatedObject(self, &topNameKey, [NSNumber numberWithFloat:enlarge], OBJC_ASSOCIATION_COPY_NONATOMIC);
    objc_setAssociatedObject(self, &rightNameKey, [NSNumber numberWithFloat:enlarge], OBJC_ASSOCIATION_COPY_NONATOMIC);
    objc_setAssociatedObject(self, &bottomNameKey, [NSNumber numberWithFloat:enlarge], OBJC_ASSOCIATION_COPY_NONATOMIC);
    objc_setAssociatedObject(self, &leftNameKey, [NSNumber numberWithFloat:enlarge], OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (void)setEnlargeEdgeWithTop:(CGFloat)top right:(CGFloat)right bottom:(CGFloat)bottom left:(CGFloat)left
{
    objc_setAssociatedObject(self, &topNameKey, [NSNumber numberWithFloat:top], OBJC_ASSOCIATION_COPY_NONATOMIC);
    objc_setAssociatedObject(self, &rightNameKey, [NSNumber numberWithFloat:right], OBJC_ASSOCIATION_COPY_NONATOMIC);
    objc_setAssociatedObject(self, &bottomNameKey, [NSNumber numberWithFloat:bottom], OBJC_ASSOCIATION_COPY_NONATOMIC);
    objc_setAssociatedObject(self, &leftNameKey, [NSNumber numberWithFloat:left], OBJC_ASSOCIATION_COPY_NONATOMIC);
}
- (CGRect) enlargedRect
{
    NSNumber* topEdge = objc_getAssociatedObject(self, &topNameKey);
    NSNumber* rightEdge = objc_getAssociatedObject(self, &rightNameKey);
    NSNumber* bottomEdge = objc_getAssociatedObject(self, &bottomNameKey);
    NSNumber* leftEdge = objc_getAssociatedObject(self, &leftNameKey);
    if (topEdge && rightEdge && bottomEdge && leftEdge)
    {
        return CGRectMake(self.bounds.origin.x - leftEdge.floatValue,
                          self.bounds.origin.y - topEdge.floatValue,
                          self.bounds.size.width + leftEdge.floatValue + rightEdge.floatValue,
                          self.bounds.size.height + topEdge.floatValue + bottomEdge.floatValue);
    }
    else
    {
        return self.bounds;
    }
}

- (UIView*) hitTest:(CGPoint) point withEvent:(UIEvent*) event
{
    CGRect rect = [self enlargedRect];
    if (CGRectEqualToRect(rect, self.bounds))
    {
        return [super hitTest:point withEvent:event];
    }
    return CGRectContainsPoint(rect, point) ? self : nil;
}  
@end





@implementation UIColor(AjenStrColor)
+ (UIColor *)colorWithAjenHexString:(NSString *)hexString {
    NSString *colorString = [[hexString stringByReplacingOccurrencesOfString: @"#" withString: @""] uppercaseString];
    CGFloat alpha, red, blue, green;
    switch ([colorString length]) {
        case 3: // #RGB
            alpha = 1.0f;
            red   = [self colorComponentFrom: colorString start: 0 length: 1];
            green = [self colorComponentFrom: colorString start: 1 length: 1];
            blue  = [self colorComponentFrom: colorString start: 2 length: 1];
            break;
        case 4: // #ARGB
            alpha = [self colorComponentFrom: colorString start: 0 length: 1];
            red   = [self colorComponentFrom: colorString start: 1 length: 1];
            green = [self colorComponentFrom: colorString start: 2 length: 1];
            blue  = [self colorComponentFrom: colorString start: 3 length: 1];
            break;
        case 6: // #RRGGBB
            alpha = 1.0f;
            red   = [self colorComponentFrom: colorString start: 0 length: 2];
            green = [self colorComponentFrom: colorString start: 2 length: 2];
            blue  = [self colorComponentFrom: colorString start: 4 length: 2];
            break;
        case 8: // #AARRGGBB
            alpha = [self colorComponentFrom: colorString start: 0 length: 2];
            red   = [self colorComponentFrom: colorString start: 2 length: 2];
            green = [self colorComponentFrom: colorString start: 4 length: 2];
            blue  = [self colorComponentFrom: colorString start: 6 length: 2];
            break;
        default:
            [NSException raise:@"Invalid color value" format: @"Color value %@ is invalid.  It should be a hex value of the form #RBG, #ARGB, #RRGGBB, or #AARRGGBB", hexString];
            break;
    }
    return [UIColor colorWithRed: red green: green blue: blue alpha: alpha];
}

+ (CGFloat) colorComponentFrom: (NSString *) string start: (NSUInteger) start length: (NSUInteger) length {
    NSString *substring = [string substringWithRange: NSMakeRange(start, length)];
    NSString *fullHex = length == 2 ? substring : [NSString stringWithFormat: @"%@%@", substring, substring];
    unsigned hexComponent;
    [[NSScanner scannerWithString: fullHex] scanHexInt: &hexComponent];
    return hexComponent / 255.0;
}

+ (UIColor*)colorWithAjenHex:(NSInteger)hexValue alpha:(CGFloat)alphaValue {
    return [UIColor colorWithRed:((float)((hexValue & 0xFF0000) >> 16))/255.0
                           green:((float)((hexValue & 0xFF00) >> 8))/255.0
                            blue:((float)(hexValue & 0xFF))/255.0
                           alpha:alphaValue];
}

@end



@implementation UIView (BorderLine)

- (UIView *)borderForColor:(UIColor *)color borderWidth:(CGFloat)borderWidth borderType:(UIBorderSideType)borderType {
    
    if (borderType == UIBorderSideTypeAll) {
        self.layer.borderWidth = borderWidth;
        self.layer.borderColor = color.CGColor;
        return self;
    }
    
    
    /// 左侧
    if (borderType & UIBorderSideTypeLeft) {
        /// 左侧线路径
        [self.layer addSublayer:[self addLineOriginPoint:CGPointMake(0.f, 0.f) toPoint:CGPointMake(0.0f, self.frame.size.height) color:color borderWidth:borderWidth]];
    }
    
    /// 右侧
    if (borderType & UIBorderSideTypeRight) {
        /// 右侧线路径
        [self.layer addSublayer:[self addLineOriginPoint:CGPointMake(self.frame.size.width, 0.0f) toPoint:CGPointMake( self.frame.size.width, self.frame.size.height) color:color borderWidth:borderWidth]];
    }
    
    /// top
    if (borderType & UIBorderSideTypeTop) {
        /// top线路径
        [self.layer addSublayer:[self addLineOriginPoint:CGPointMake(0.0f, 0.0f) toPoint:CGPointMake(self.frame.size.width, 0.0f) color:color borderWidth:borderWidth]];
    }
    
    /// bottom
    if (borderType & UIBorderSideTypeBottom) {
        /// bottom线路径
        [self.layer addSublayer:[self addLineOriginPoint:CGPointMake(0.0f, self.frame.size.height) toPoint:CGPointMake( self.frame.size.width, self.frame.size.height) color:color borderWidth:borderWidth]];
    }
    
    return self;
}

- (CAShapeLayer *)addLineOriginPoint:(CGPoint)p0 toPoint:(CGPoint)p1 color:(UIColor *)color borderWidth:(CGFloat)borderWidth {
    
    /// 线的路径
    UIBezierPath * bezierPath = [UIBezierPath bezierPath];
    [bezierPath moveToPoint:p0];
    [bezierPath addLineToPoint:p1];
    
    CAShapeLayer * shapeLayer = [CAShapeLayer layer];
    shapeLayer.strokeColor = color.CGColor;
    shapeLayer.fillColor  = [UIColor clearColor].CGColor;
    /// 添加路径
    shapeLayer.path = bezierPath.CGPath;
    /// 线宽度
    shapeLayer.lineWidth = borderWidth;
    return shapeLayer;
}


@end  


@implementation UITableViewCell (XIB)

+(UITableViewCell *)loadXibCellWithTableView:(UITableView *)tableView iden:(NSString *)iden{
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:iden];
    if (!cell) {
        cell =  [[NSBundle mainBundle]loadNibNamed:iden owner:self options:nil].firstObject;
    }
    return cell;
}

@end
